package com.beeantec.jnrerc.modules.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;

import com.beeantec.jnrerc.dto.MemberVo;
import com.beeantec.jnrerc.dto.request.GroupJoinReqDTO;
import com.beeantec.jnrerc.dto.request.GroupReqDTO;
import com.beeantec.jnrerc.dto.request.InvitedJoinReqDTO;

import com.beeantec.jnrerc.dto.response.GroupMemberRespDTO;
import com.beeantec.jnrerc.dto.response.GroupRespDTO;

import com.beeantec.jnrerc.entity.*;
import com.beeantec.jnrerc.modules.course.service.GroupMemberService;
import com.beeantec.jnrerc.modules.course.service.GroupService;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程分类
 *
 * @author Anthony
 */
@RestController
@RequestMapping(value = "/api/coursegroup")
@Api(value = "课程学习小组", tags = "课程学习小组")
public class CourseGroupController extends BaseController {
    @Autowired
    GroupService groupService;

    @Autowired
    GroupMemberService groupMemberService;

    @Autowired
    MemberService memberService;
    /**
     * 获取课程所有学习小组接口
     *
     * @author Anthony
     */
    @ApiOperation(value = "获取课程所有学习小组", notes = "获取课程所有学习小组")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ApiRest<List<GroupRespDTO>> listByCourseId(@RequestParam("courseId") Integer courseId) {
        List<GroupRespDTO> groupRespDTOList = new ArrayList<>();
        QueryWrapper<CourseGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CourseGroup::getCorId,courseId);
        List<CourseGroup> groupList =  new ArrayList<>();
        groupList=   groupService.getBaseMapper().selectList(queryWrapper);
           for (CourseGroup group:groupList){
               List<GroupMemberRespDTO> groupMemberList =  groupMemberService.getGroupMemberList(group.getId(),courseId.toString());
               GroupRespDTO groupRespDTO = new GroupRespDTO();
               BeanUtils.copyProperties(group,groupRespDTO);
               groupRespDTO.setGroupMemberList(groupMemberList);
               groupRespDTO.setGName(group.getTeamName());
               groupRespDTO.setCId(courseId.toString());
               groupRespDTOList.add(groupRespDTO);
           }
        return success(groupRespDTOList);
    }





    /**
     * 创建学习小组
     *
     * @author Anthony
     */
    @ApiOperation(value = "创建学习小组", notes = "创建学习小组")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ApiRest<String> create(@RequestBody GroupReqDTO groupReqDTO) {
        QueryWrapper<CourseGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CourseGroup::getCorId,groupReqDTO.getCourseId())
                .eq(CourseGroup::getTeamName,groupReqDTO.getName());

        QueryWrapper<GroupMember> groupMemberQueryWrapper = new QueryWrapper<>();
        groupMemberQueryWrapper.lambda().eq(GroupMember::getGroupId,groupReqDTO.getCourseId())
                .eq(GroupMember::getCreator,groupReqDTO.getMemberId());
       GroupMember groupMember1 = groupMemberService.getOne(groupMemberQueryWrapper);
       if (null!=groupMember1){
           super.failure("请勿重复创建学习小组");
       }
        CourseGroup group = groupService.getOne(queryWrapper);
        if (null!=group){
            super.failure("已存在相同小组名称");
        }
        group = new CourseGroup();
        group.setCorId(groupReqDTO.getCourseId());
        group.setTeamName(groupReqDTO.getName());
       int  result=  groupService.getBaseMapper().insert(group);
       if (result>0){
           QueryWrapper<CourseGroup> queryWrapper1 = new QueryWrapper<>();
           queryWrapper1.lambda().eq(CourseGroup::getCorId,groupReqDTO.getCourseId())
                   .eq(CourseGroup::getTeamName,groupReqDTO.getName());
           CourseGroup courseGroup = groupService.getOne(queryWrapper1);
           GroupMember groupMember = new GroupMember();
           groupMember.setCreator(1);
           groupMember.setGroupId(courseGroup.getId());
           groupMember.setCourseId(groupReqDTO.getCourseId().toString());
           groupMember.setMemberId(groupReqDTO.getMemberId());
          boolean gResult =  groupMemberService.save(groupMember);
          if (!gResult){
              super.failure("创建学习小组失败");
          }
       }else {
           super.failure("创建学习小组失败");
       }
        return success("创建学习小组成功");
    }


    /**
     * 加入学习小组
     *
     * @author Anthony
     */
    @ApiOperation(value = "加入学习小组", notes = "加入学习小组")
    @RequestMapping(value = "/join", method = RequestMethod.POST)
    public ApiRest<String> join(@RequestBody GroupJoinReqDTO groupJoinReqDTO) {
        List<GroupMemberRespDTO> groupMemberRespDTOList = groupMemberService.getGroupMemberList(groupJoinReqDTO.getId(),groupJoinReqDTO.getCId());
        if (null!=groupMemberRespDTOList){
            for (GroupMemberRespDTO groupMemberRespDTO:groupMemberRespDTOList){
                if (groupMemberRespDTO.getMemberId()==groupJoinReqDTO.getMemberId()){
                    return super.failure("请勿重复加入学习小组");
                }
            }
        }
        GroupMember groupMember = new GroupMember();
        groupMember.setCreator(0);
        groupMember.setGroupId(groupJoinReqDTO.getId());
        groupMember.setMemberId(groupJoinReqDTO.getMemberId());
        groupMember.setCourseId(groupJoinReqDTO.getCId());
        boolean result = groupMemberService.save(groupMember);
        if (!result){
            return super.failure("加入学习小组失败");
        }
        return success("加入学习小组成功");
    }



    /**
     * 邀请加入学习小组
     *
     * @author Anthony
     */
    @ApiOperation(value = "邀请加入学习小组", notes = "加入学习小组")
    @RequestMapping(value = "/invitedJoin", method = RequestMethod.POST)
    public ApiRest<String> invitedJoin(@RequestBody InvitedJoinReqDTO invitedJoinReqDTO) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Member::getWxOpenid,invitedJoinReqDTO.getOpenId());
        Member member = memberService.getOne(queryWrapper);
        if (null==member){
            return super.failure("邀请用户不存在");
        }
        GroupMember groupMember = new GroupMember();
        groupMember.setCreator(0);
        groupMember.setGroupId(invitedJoinReqDTO.getId());
        groupMember.setMemberId(member.getMemberId());
        groupMember.setCourseId(invitedJoinReqDTO.getCId());
        boolean result = groupMemberService.save(groupMember);
        if (!result){
            return super.failure("加入学习小组失败");
        }
        return success("加入学习小组成功");
    }



}
